<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="管理员ID" prop="adminId">
          <el-input
            v-model="queryParams.adminId"
            placeholder="请输入管理员ID"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作接口" prop="interfaceName">
          <el-input
            v-model="queryParams.interfaceName"
            placeholder="请输入操作接口"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

  
      <el-table v-loading="loading" :data="LogList">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="日志编号" prop="id" width="120" />
        <el-table-column label="管理员ID" prop="adminId" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="操作接口" prop="interfaceName" :show-overflow-tooltip="true" width="200" />
        <el-table-column label="操作时间" prop="operationTime" width="250" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon=""
              @click="LogDetails(scope.row)"
              v-hasPermi="['system:role:remove']"
            >日志详情</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      
      <!-- 日志详情 -->
      <el-dialog title="日志详情" :visible.sync="openDetails" width="1000px" append-to-body>
        <el-form ref="Details" :model="Details"  label-width="100px">
          <el-row>
                  <el-col :span="12">
                    <el-form-item label="日志ID" prop="id">
                        <el-input v-model="Details.id"  :disabled="true" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="管理员ID" prop="adminId">
                       <el-input v-model="Details.adminId"  :disabled="true" />
                     </el-form-item>
                  </el-col>
      
          </el-row>
          <el-row>
                  <el-col :span="12">
                    <el-form-item label="操作名" prop="action">
                        <el-input v-model="Details.action"  :disabled="true" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="操作日期" prop="operationTime">
                       <el-input v-model="Details.operationTime"  :disabled="true" />
                     </el-form-item>
                  </el-col>
      
          </el-row>
          <el-row>
                  <el-col :span="8">
                    <el-form-item label="操作接口" prop="interfaceName">
                        <el-input v-model="Details.interfaceName"  :disabled="true" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="花费时间" prop="cost_time">
                       <el-input v-model="Details.cost_time"  :disabled="true" />
                     </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="返回值" prop="return_value">
                       <el-input v-model="Details.return_value"  :disabled="true" />
                     </el-form-item>
                  </el-col>
          </el-row>
                  <el-form-item label="参数" prop="param">
                    <el-input v-model="paramDisplay"  :disabled="true" />
                  </el-form-item>
   
        </el-form>
      </el-dialog>
  
  
    </div>
  </template>
  
  <script>
  import {getLogList} from "@/api/didi/data";
  
  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 角色表格数据
        LogList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        openDetails :false,
        // 日期范围
        dateRange: [],
       // 数据范围选项

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          adminId: "",
          interfaceName: "",
        },
         paramDisplay: "",
        // 表单参数
        form: {},
        Details:{},
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询角色列表 */
      getList() {
        this.loading = true;
        getLogList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
       
            this.LogList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      LogDetails(row){
        try {
        // 解析 operationRecord
        const data = JSON.parse(row.operationRecord);

        // 解析 params 数组中的每个元素
        const data1 = data.params.map(item => {
          try {
            return JSON.parse(item);
          } catch (parseError) {
            console.error('解析 params 数组元素时出错:', parseError);
            return null;
          }
        });

    

        // 将 row 中的其他数据合并到 Details 中
        for (const key in row) {
          if (key!== 'operationRecord') {
            this.Details[key] = row[key];
          }
        }

        // 将 data 中的其他数据合并到 Details 中
        for (const key in data) {
          if (key!== 'params') {
            this.Details[key] = data[key];
          }
        }

        // 将 data1 赋值到 Details 中
        this.Details.param = data1;

        console.log(this.Details);
      } catch (error) {
        console.error('处理数据时出错:', error);
      }
         
      this.updateParamDisplay();
        this.openDetails=!this.openDetails
  
      },

      updateParamDisplay() {
        const paramArray = this.Details.param;
        const displayArray = paramArray.map((obj) => {
          const values = [];
          for (const key in obj) {
            values.push(`${key}: ${obj[key]}`);
          }
          return values.join(", ");
        });
        this.paramDisplay = displayArray.join("; ");
      },
   
      // 取消按钮
      cancel() {
        this.open = false;
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },

  
    }
  }
  
  </script>
  <style>

 </style>